/*
 * DG (DG Gaming Engine)  - API
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.client.gui;

import java.awt.Color;
import java.awt.Component;
import java.util.*;

import javax.swing.*;

import info.clearthought.layout.TableLayout;
import com.dg.gaming.api.common.Game;
import com.dg.gaming.api.common.User;

/**
 * Visual list of all the users logged on.
 *
 * @author  Bob Marks
 * @version Alpha 0.2.3
 */
public class JUserList extends JList implements Observer {

    // Data (user list)
    private Game game = null;

    // Excluded user
    private User excludedUser = null;

    /**
     * Empty Constructor.
     */
    public JUserList(Game game) {
        super();

        // Sets fields
        this.game = game;

        init();
    }

    /**
     * Constructor with excluded user.
     */
    public JUserList(Game game, User excludedUser) {
        super();

        // Sets fields
        this.game = game;
        this.excludedUser = excludedUser;

        init();
    }

    /**
     * Init
     */
    private void init() {

        // Add an observer to the userList
        this.game.getUserList().addObserver(this);
        this.game.getTableList().addObserver(this);

        // Set the renderer of this user list
        setCellRenderer(new UserListRenderer());

        // Refresh the GUI
        refresh();
    }

    /**
     * Return the selected user as an object.
     *
     * @return   Selected user object.
     */
    public User getSelectedUser() {
        return (User) getSelectedValue();
    }

    /**
     * Return the selected user's username.
     *
     * @return   Selected user's username.
     */
    public long getSelectedUsername() {
        return ((User) getSelectedValue()).getUserid();
    }

    /**
     * Refresh the user list.
     */
    public void refresh() {
        if (game != null) {

            Vector userObjects = game.getUserList().getUserObjects();
            if (this.excludedUser != null) {
                userObjects.remove(this.excludedUser);
            }

            setListData(userObjects);
            repaint();
        }
    }

    /**
     * Refresh when the data has been updated on the userlist.
     *
     * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
     */
    public void update(Observable obs, Object str) {
        refresh();
    }

    /**
     * ===========================================================
     * Custom renderer for the pulldown.
     * ===========================================================
     */
    class UserListRenderer extends JPanel implements ListCellRenderer {

        private UserLabel userLabel;
        private DGLabel ratingLabel, tablesLabel;

        // Declare colours
        private Color bgColour1, bgColour2;

        /**
         * Constructor for this renderer.
         */
        public UserListRenderer() {
            // Set layout
            double pref = TableLayout.PREFERRED, fill = TableLayout.FILL;
            double[][] sizes = { {fill, pref, 36, 4}, {pref}
            };
            setLayout(new TableLayout(sizes));

            // Create components and and labels
            userLabel = new UserLabel();
            ratingLabel = new DGLabel();
            tablesLabel = new DGLabel();

            // Set font of labels
            ratingLabel.setFont(DGAwt.LIST_FONT);
            tablesLabel.setFont(DGAwt.LIST_FONT);

            add(userLabel, "0,0,l,c");
            add(tablesLabel, "1,0,r,c");
            add(ratingLabel, "2,0,r,c");
        }

        /* (non-Javadoc)
         * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
         */
        public Component getListCellRendererComponent(JList list,
                Object value,
                int index,
                boolean isSelected,
                boolean cellHasFocus) {
            // Set colours up
            if (isSelected) {
                setBackground(new Color(240, 240, 240));
                setForeground(Color.black);
            } else {
                setBackground(Color.white);
                setForeground(Color.black);
            }

            // Set label text and square colour.
            User user = (User) value;

            if (user != null) {
                // Extract username
                long username = user.getUserid();

                // Create rating square, rating label and username
                userLabel.refresh(user);
                ratingLabel.setText(String.valueOf(user.getRating()));

                // Compute table numbers
                String[] userTables = game.getTableList().getTableNumsForUser(
                        username);
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < userTables.length; i++) {
                    sb.append(String.valueOf(userTables[i]));
                    if (i < userTables.length - 1) {
                        sb.append(",");
                    }
                }
                tablesLabel.setText(sb.toString());
            }

            return this;
        }
    }
}
